﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.MT3DMS.DomainLayer;
using HA.ModFlow.Controls.MT3DMSWindows;

using System.Collections.ObjectModel;

using System.Windows.Input;

using System.Windows;

using HA.ModFlow.Infrastructure;
using HA.Data;
using HA.ModFlow.Controls;


namespace HA.ModFlow.Controls
{
    public class PCChemicalReaction
    {

        public PCChemicalReaction( Project proj)
        {
            MT3DMSData mtData = proj.Mt3dmsData;
            this._chemicalReaction = (ChemicalReaction)mtData.ChemicalReaction.Clone();


            this._species = new ObservableCollection<PCChemicalSpecies>();

            foreach (ChemicalSpecies item in this._chemicalReaction.ChemicalSpecies)
            {
                PCChemicalSpecies pc = new PCChemicalSpecies(item,mtData.BasicTransport.ModelOption,proj);

                this._species.Add(pc);

            }


        }




        ChemicalReaction _chemicalReaction;

        public ChemicalReaction ChemicalReaction
        {
            get { return _chemicalReaction; }
            set { _chemicalReaction = value; }
        }


        ObservableCollection<PCChemicalSpecies> _species;

        public ObservableCollection<PCChemicalSpecies> Species
        {
            get { return _species; }
            set { _species = value; }
        }



        /// <summary>
        /// 保存对应的项
        /// </summary>
        public void Confirm()
        {

            foreach (PCChemicalSpecies item in this._species)
            {
                item.Confirm();
                
            }

        }



        public static Window mainWindow;


    }


    public class PCChemicalSpecies
    {

        public PCChemicalSpecies(ChemicalSpecies sp,MTModelOption mdOption ,Project proj)
        {

            this._spe = sp;



            this._editIC = new EditICCommand(this._spe.SrcConc, this._spe.SpName, mdOption, proj);
            this._editSP1 = new EditSP1Command(this._spe.Sp1, this._spe.SpName, mdOption, proj);
            this._editSP2 = new EditSP2Command(this._spe.Sp2, this._spe.SpName, mdOption, proj);
            this._editRC1 = new EditRC1Command(this._spe.Rc1, this._spe.SpName, mdOption, proj);
            this._editRC2 = new EditRC2Command(this._spe.Rc2, this._spe.SpName, mdOption, proj);
        }


        public string Name
        {
            get
            {

                return this._spe.SpName;
            }
        }

   

        ChemicalSpecies _spe;

        public ChemicalSpecies Spe
        {
            get { return _spe; }
            set { _spe = value; }
        }


        EditICCommand _editIC;

        public EditICCommand EditIC
        {
            get { return _editIC; }
            set { _editIC = value; }
        }

        EditSP1Command _editSP1;

        public EditSP1Command EditSP1
        {
            get { return _editSP1; }
            set { _editSP1 = value; }
        }
        EditSP2Command _editSP2;

        public EditSP2Command EditSP2
        {
            get { return _editSP2; }
            set { _editSP2 = value; }
        }
        EditRC1Command _editRC1;

        public EditRC1Command EditRC1
        {
            get { return _editRC1; }
            set { _editRC1 = value; }
        }
        EditRC2Command _editRC2;

        public EditRC2Command EditRC2
        {
            get { return _editRC2; }
            set { _editRC2 = value; }
        }



        public void Confirm()
        {

          



            this._spe.SrcConc = this._editIC.Arry;
            this._spe.Sp1 = this._editSP1.Arry;

            this._spe.Sp2 = this._editSP2.Arry;

            this._spe.Rc1 = this._editRC1.Arry;

            this._spe.Rc2 = this._editRC2.Arry;

           






 

        }
    }
   

    public class EditICCommand : ICommand
    {
        public EditICCommand(List<double[,]> arry, string name, MTModelOption mdOption, Project proj)
        {
            project = proj;
            this._arry = arry;
            this._name = name;
            _mdoption = mdOption;
        }

        Project project;
        MTModelOption _mdoption;
        private List<double[,]> _arry;

        public List<double[,]> Arry
        {
            get { return _arry; }
            set { _arry = value; }
        }

        private bool _isEnabled;
        /// <summary>
        /// 根据选项处理灰化关系标记
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }
   

        private string _name;
        public bool CanExecute(object parameter)
        {
            return true  ;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {          
            //winConstantArry win = new winConstantArry(this ._arry);
            //win.Title = _name + "_IC" ;
            //win.Owner = PCChemicalReaction.mainWindow;
            //win.ShowDialog();
            int nrows;
            int ncols;
            int nlayers;

            nlayers = this._arry.Count;

            nrows = this._arry[0].GetLength(0);
            ncols = this._arry[0].GetLength(1);
            

          
            CellCenterDataSet3d ds = new CellCenterDataSet3d(this._arry, null,nrows,ncols,nlayers);

            ds.Label = _name + " 初始浓度";

            winCellCenterDSEdit win = new winCellCenterDSEdit(ds,project);
            win.Owner = PCChemicalReaction.mainWindow;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetConcentration(_mdoption.Units) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog() == true)
            {
               this._arry=  win.PcDs.Dataset.ToListData();
               if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                   project.OnCurrentDSChanged();
            }



        }
    }
}

    public class EditSP1Command : ICommand
    {
        public EditSP1Command(List<double[,]> arry, string name, MTModelOption mdOption,Project proj )
        {
            project = proj;
            this._arry = arry;
            this._name = name;
            this._mdOption = mdOption;
        }
        Project project;
        MTModelOption _mdOption;
        private List<double[,]> _arry;

        public List<double[,]> Arry
        {
            get { return _arry; }
            set { _arry = value; }
        }
        private bool _isEnabled;
        /// <summary>
        /// 根据选项处理灰化关系标记
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }

        private string _name;
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            //winConstantArry win = new winConstantArry(this._arry);
            //win.Title = _name + "_SP1";
            //win.Owner =HA.ModFlow.Controls. PCChemicalReaction.mainWindow;
            //win.ShowDialog();



            int nrows;
            int ncols;
            int nlayers;

            nlayers = this._arry.Count;

            nrows = this._arry[0].GetLength(0);
            ncols = this._arry[0].GetLength(1);



            CellCenterDataSet3d ds = new CellCenterDataSet3d(this._arry, null, nrows, ncols, nlayers);

            ds.Label = _name + " 第一吸附参数";

            winCellCenterDSEdit win = new winCellCenterDSEdit(ds,project);
            win.Owner = PCChemicalReaction.mainWindow;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + "^3/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetMass(_mdOption.Units.Mass) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog() == true)
            {
                this._arry = win.PcDs.Dataset.ToListData();
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }


        }
    }

    public class EditSP2Command : ICommand
    {
        public EditSP2Command(List<double[,]> arry, string name, MTModelOption mdOption,Project proj)
        {
            project=proj ;
            this._arry = arry;
            this._name = name;
            _mdoption = mdOption;
        }
        Project project;
        MTModelOption _mdoption;
        private List<double[,]> _arry;

        public List<double[,]> Arry
        {
            get { return _arry; }
            set { _arry = value; }
        }
        private bool _isEnabled;
        /// <summary>
        /// 根据选项处理灰化关系标记
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }

        private string _name;
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            //winConstantArry win = new winConstantArry(this._arry);
            //win.Title = _name + "_SP2";
            //win.Owner = HA.ModFlow.Controls.PCChemicalReaction.mainWindow;
            //win.ShowDialog();


            int nrows;
            int ncols;
            int nlayers;

            nlayers = this._arry.Count;

            nrows = this._arry[0].GetLength(0);
            ncols = this._arry[0].GetLength(1);



            CellCenterDataSet3d ds = new CellCenterDataSet3d(this._arry, null, nrows, ncols, nlayers);

            ds.Label = _name + " 第二吸附或双区域模型参数";

            winCellCenterDSEdit win = new winCellCenterDSEdit(ds,project);
            win.Owner = PCChemicalReaction.mainWindow;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(_mdoption.Units.Time) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog() == true)
            {
                this._arry = win.PcDs.Dataset.ToListData();
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }



        }
    }
    public class EditRC1Command : ICommand
    {
        public EditRC1Command(List<double[,]> arry, string name, MTModelOption mdOption,Project proj)
        {
            project = proj;
            this._arry = arry;
            this._name = name;
            _mdoption = mdOption;
        }
        Project project;
        MTModelOption _mdoption;
        private List<double[,]> _arry;

        public List<double[,]> Arry
        {
            get { return _arry; }
            set { _arry = value; }
        }
        private string _name;
        public bool CanExecute(object parameter)
        {
            return true;
        }

        private bool _isEnabled;
        /// <summary>
        /// 根据选项处理灰化关系标记
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }
        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {



            //winConstantArry win = new winConstantArry(this._arry);
            //win.Title = _name + "_RC1";
            //win.Owner = HA.ModFlow.Controls.PCChemicalReaction.mainWindow;
            //win.ShowDialog();


            int nrows;
            int ncols;
            int nlayers;

            nlayers = this._arry.Count;

            nrows = this._arry[0].GetLength(0);
            ncols = this._arry[0].GetLength(1);



            CellCenterDataSet3d ds = new CellCenterDataSet3d(this._arry, null, nrows, ncols, nlayers);

            ds.Label = _name + " 溶解状态的一阶反应率";

            winCellCenterDSEdit win = new winCellCenterDSEdit(ds,project);
            win.Owner = PCChemicalReaction.mainWindow;
            win.ResizeMode = ResizeMode.NoResize;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            win.Unit += "(1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(_mdoption.Units.Time) + ")";
            if (win.ShowDialog() == true)
            {
                this._arry = win.PcDs.Dataset.ToListData();
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }



        }
    }
    public class EditRC2Command : ICommand
    {
        public EditRC2Command(List<double[,]> arry, string name, MTModelOption mdOption,Project proj)
        {
            project = proj; 
            this._arry = arry;
            this._name = name;
            _mdoption = mdOption;
        }
        Project project;
        MTModelOption _mdoption;
        private List<double[,]> _arry;

        public List<double[,]> Arry
        {
            get { return _arry; }
            set { _arry = value; }
        }
        private bool _isEnabled;
        /// <summary>
        /// 根据选项处理灰化关系标记
        /// </summary>
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }

        private string _name;
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            //winConstantArry win = new winConstantArry(this._arry);
            //win.Title = _name + "_RC2";
            //win.Owner = HA.ModFlow.Controls.PCChemicalReaction.mainWindow;
            //win.ShowDialog();


            int nrows;
            int ncols;
            int nlayers;

            nlayers = this._arry.Count;

            nrows = this._arry[0].GetLength(0);
            ncols = this._arry[0].GetLength(1);



            CellCenterDataSet3d ds = new CellCenterDataSet3d(this._arry, null, nrows, ncols, nlayers);

            ds.Label = _name + " 吸附状态的一阶反应率";

            winCellCenterDSEdit win = new winCellCenterDSEdit(ds,project);
            win.Owner = PCChemicalReaction.mainWindow;
            win.ResizeMode = ResizeMode.NoResize;
            win.Unit += "(1/" + HA.ModFlow.Domain.DomainLayer.UnitConverter.GetTime(_mdoption.Units.Time) + ")";
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            if (win.ShowDialog() == true)
            {
                this._arry = win.PcDs.Dataset.ToListData();
                if (project.CurrentDSData != null && project.CurrentDSData.Label == ds.Label)
                    project.OnCurrentDSChanged();
            }

        }
    }
